// 导入数据库模块
const db = require("../db/index");

// 上传手机图片
exports.uploadPhonePic = (req, res) => {
    res.send("ok");
};

// 上传手机信息
exports.addPhone = (req, res) => {
    const time = new Date();
    const phoneDetail = {
        id: String(time.getTime()),
        ...req.body.form,
        phone_pic: "phone-simple/" + req.body.form.phone_pic,
    };
    const sql = `insert into phone_params set ?`;
    // 导入数据库操作模块
    const db = require("../db/index");
    // 执行 SQL 语句
    db.query(sql, phoneDetail, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        // 执行 SQL 语句成功，但是影响行数不等于 1
        if (results.affectedRows !== 1) return res.cc("添加失败失败！");
        // 发布文章成功
        res.cc("添加成功", 0);
    });
};

// 删除手机信息
exports.delPhone = (req, res) => {
    const { phone_name } = req.body;
    const sql = `delete from phone_params where phone_name=?`;
    // 导入数据库操作模块
    const db = require("../db/index");
    // 执行 SQL 语句
    db.query(sql, phone_name, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        // 执行 SQL 语句成功，但是影响行数不等于 1
        if (results.affectedRows !== 1) return res.cc("删除失败！");

        res.cc("删除成功", 0);
    });
};

// 获取品牌信息
exports.getbrandInfo = (req, res) => {
    // res.send('ok');
    // 从数据库中获取到接口
    const sql = "select id,brand,brand_logo from phone_brands";
    db.query(sql, (err, results) => {
        // 执行sql语句失败
        if (err) return res.cc(err);
        // 成功
        res.send({
            status: 0,
            message: "获取手机静态信息成功",
            data: results,
        });
    });
};

// 获取手机信息
exports.getphoneInfo = (req, res) => {
    // res.send('ok');
    // 从数据库中获取到接口
    const sql = "select * from phone_params";
    // const sql = 'select brand_logo from phone_brands';
    db.query(sql, (err, results) => {
        // 执行sql语句失败
        if (err) return res.cc(err);
        // 成功
        res.send({
            status: 0,
            message: "获取手机静态信息成功",
            data: results,
        });
    });
};

// 模糊查询手机
exports.fuzzyGetName = (req, res) => {
    const { fuzzyVal } = req.query;
    if (fuzzyVal) {
        const sql = `select * from phone_params where phone_name like '%${fuzzyVal}%'`;
        db.query(sql, (err, results) => {
            // 执行sql语句失败
            if (err) return res.cc(err);
            // 成功
            res.send({
                status: 0,
                message: "获取手机静态信息成功",
                data: results,
            });
        });
    }
};

// 获取从高到低排序的前二十台手机(条件从前端接收)
exports.getphoneRank = (req, res) => {
    const { rankReason } = req.query;

    let sql = null;

    switch (rankReason) {
        case "price":
            sql = "select * from phone_params order by price DESC limit 0,20";
            break;
        case "points":
            sql = "select * from phone_params order by points DESC limit 0,20";
            break;
        case "frame_rate":
            sql = "select * from phone_params order by frame_rate DESC limit 0,20";
            break;
        case "feel":
            sql =
                "SELECT id,phone_name,(8-thick) * 0.85 + (180-weight)/18+(73-width)+10 AS 'feel' FROM `phone_params`";
            break;
        case "battery":
            sql = "select * from phone_params order by battery DESC limit 0,20";
            break;
        default:
            break;
    }

    if (sql) {
        db.query(sql, (err, results) => {
            // 执行sql语句失败
            if (err) return res.cc(err);
            if (rankReason === "feel") {
                results = results
                    .map((item) => {
                        return {
                            id: item.id,
                            phone_name: item.phone_name,
                            feel: item.feel.toFixed(2),
                        };
                    })
                    .sort((a, b) => b.feel - a.feel)
                    .slice(0, 20);
            }

            // 成功
            res.send({
                status: 0,
                message: "获取手机排名成功",
                data: results,
            });
        });
    } else {
        res.send({
            status: 1,
            message: "条件错误或不存在！",
        });
    }
};